package services;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;

import models.RegionLevel;
import models.RegionLevel;
import models.mappers.RegionLevelMapper;
import models.mappers.RegionLevelMapper;
import vos.RegionLevelSeachVo;
import vos.RegionLevelSeachVo;

/**
 * 经销商城市登记
 * 
 * @ClassName: RegionLevelService 
 * @author: wangsm
 * @date: 2016年5月31日 上午10:18:57
 */
public class RegionLevelService {
	/**
	 * 获取下拉列表
	 *
	 * @Title: getCombobox 
	 * @return
	 * @return: List<RegionLevel>
	 */
	public static List<RegionLevel> getCombobox() {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
            return mapper.getCombobox();
        } finally {
            ss.close();
        }
	}
	/**
	 * 统计经销商城市等级
	 *
	 * @Title: countRegionLevel 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countRegionLevel(RegionLevelSeachVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
            return mapper.countRegionLevel();
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取经销商城市等级列表
	 *
	 * @Title: listRegionLevel 
	 * @param vo
	 * @return
	 * @return: List<RegionVo>
	 */
	public static List<RegionLevel> listRegionLevel(RegionLevelSeachVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
            return mapper.listRegionLevel(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @param ids 选中记录id
	 * @param operatorid 
	 * @return: void
	 */
	public static Boolean betchEnable(String ids, String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
            mapper.betchEnable(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @param listid 选中记录id
	 * @return: void
	 */
	public static Boolean betchDisable(List<String> listid,String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
            mapper.betchDisable(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchDel 
	 * @param ids
	 * @return
	 * @return: boolean
	 */
	public static boolean betchDel(List<String> listid) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
            mapper.betchDel(map);
            ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 删除城市等级
	 *
	 * @Title: delRegionLevel 
	 * @param id 城市等级id
	 * @return
	 * @return: boolean
	 */
	public static boolean delRegionLevel(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
	        mapper.delRegionLevel(id);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		
		return true;
	}
	/**
	 * 根据id获取城市等级信息
	 *
	 * @Title: getById 
	 * @param levelid 城市等级id
	 * @return
	 * @return: RegionLevel
	 */
	public static RegionLevel getById(Long levelid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
			return mapper.getById(levelid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存城市等级信息
	 *
	 * @Title: saveRegionLevel 
	 * @param device 城市等级对象
	 * @return
	 * @return: boolean
	 */
	public static boolean saveRegionLevel(RegionLevel device) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
			Date date=new Date();
			device.createtime=date;
	        mapper.saveRegionLevel(device);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 更新城市等级信息
	 *
	 * @Title: updateRegionLevel 
	 * @param device
	 * @return
	 * @return: boolean
	 */
	public static boolean updateRegionLevel(RegionLevel device) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
			Date date=new Date();
			device.modifytime=date;
	        mapper.updateRegionLevel(device);
	        ss.commit();
		}
		catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 导出excel
	 *
	 * @Title: exportXls 
	 * @param vo
	 * @return
	 * @return: List<RegionLevel>
	 */
	public static List<RegionLevel> exportXls(RegionLevelSeachVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RegionLevelMapper mapper = ss.getMapper(RegionLevelMapper.class);
            return mapper.exportXls(vo);
        } finally {
            ss.close();
        }
	}
}
